home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / hdf / mac / h10srcst.hqx / Source / HyperHell Source / obj / tekmain.c.OLDCOPY
Encoding:
Text File  |  1989-01-09  |  14.0 KB  |  600 lines

  1. /*
  2.  * File Sample.c
  3.  *
  4.  * Copyright Apple Computer, Inc. 1985-1987
  5.  * All rights reserved.
  6.  *
  7.  * Sample application in C
  8.  *
  9.  *    This sample displays a fixed sized window in which the user can
  10.  *    enter and edit text.  The style and layout of the procedures
  11.  *    reflects C programming style, and differs somewhat from the
  12.  *    simple example Pascal prgram outlined in "Inside Macintosh".
  13.  *
  14.  *    The C Sample includes a few minor additions to the Pascal sample:
  15.  *    [1] About Sample... dialog box supported.
  16.  *    [2] The I-Beam cursor is set when inside the window
  17.  *    [3] Various bugs are fixed concerning the current port, cut/copy/paste
  18.  *        to/from the clipboard, a few random crasher bugs
  19.  *    [4] Segmentation is demonstrated
  20.  */
  21.  
  22. #include    <types.h>
  23. #include    <quickdraw.h>
  24. #include    <toolutils.h>
  25. #include    <fonts.h>
  26. #include    <events.h>
  27. #include    <windows.h>
  28. #include    <dialogs.h>
  29. #include    <menus.h>
  30. #include    <desk.h>
  31. #include    <textedit.h>
  32. #include    <scrap.h>
  33. #include    <segload.h>
  34.  
  35. extern _DataInit();
  36.  
  37. /*
  38.  * Resource ID constants.
  39.  */
  40. #define    appleID            128             /* This is a resource ID */
  41. #define    fileID             129             /* ditto */
  42. #define    editID             130             /* ditto */
  43.  
  44. #define    appleMenu        0                /* MyMenus[] array indexes */
  45. #define    aboutMeCommand    1
  46.  
  47. #define    fileMenu        1
  48. #define    quitCommand     1
  49.  
  50. #define    editMenu        2
  51. #define    undoCommand     1
  52. #define    cutCommand        3
  53. #define    copyCommand     4
  54. #define    pasteCommand    5
  55. #define    clearCommand    6
  56.  
  57. #define menuCount         3
  58. /*
  59.  * For the one and only text window
  60.  */
  61. #define windowID        128
  62. /*
  63.  * For the About Sample... DLOG
  64.  */
  65. #define    aboutMeDLOG        128
  66. #define    okButton        1
  67. #define    authorItem        2            /* For SetIText */
  68. #define    languageItem    3            /* For SetIText */
  69.  
  70. /*
  71.  * C programs typically use macros for simple expressions which
  72.  * must be function calls in Pascal.  Here are a couple of examples:
  73.  */
  74.  
  75. /*
  76.  * Inline SetRect() macro, efficient when (rectp) is a constant.
  77.  * Must not be used if (rectp) has side effects.
  78.  *
  79.  * We could do an InsetRect() macro in a similar vein.
  80.  */
  81. #define SETRECT(rectp, _left, _top, _right, _bottom)    \
  82.     (rectp)->left = (_left), (rectp)->top = (_top),     \
  83.     (rectp)->right = (_right), (rectp)->bottom = (_bottom)
  84.  
  85. /*
  86.  * HIWORD and LOWORD macros, for readability.
  87.  */
  88. #define HIWORD(aLong)        (((aLong) >> 16) & 0xFFFF)
  89. #define LOWORD(aLong)        ((aLong) & 0xFFFF)
  90.  
  91. /*
  92.  * Global Data objects, used by routines external to main().
  93.  */
  94. MenuHandle        MyMenus[menuCount];     /* The menu handles */
  95. Boolean         DoneFlag;                /* Becomes TRUE when File/Quit chosen */
  96. TEHandle        TextH;                    /* The TextEdit handle */
  97.  
  98.  
  99.  
  100. int main()
  101. {
  102.     Rect                    screenRect;
  103.     Rect                    dragRect;
  104.     Rect                    txRect;
  105.     Point                    mousePt;
  106.     CursHandle                ibeamHdl;
  107.     EventRecord             myEvent;
  108.     WindowPtr                theActiveWindow;
  109.     WindowPtr                whichWindow;
  110.     register    WindowPtr    myWindow;            /* Referenced often */
  111.     WindowRecord            wRecord;
  112.     extern void             setupMenus();
  113.     extern void             doCommand();
  114.  
  115.     /*
  116.      * Initialization traps
  117.      */
  118.     UnloadSeg(_DataInit);
  119.     InitGraf(&qd.thePort);
  120.     InitFonts();
  121.     FlushEvents(everyEvent, 0);
  122.     InitWindows();
  123.     InitMenus();
  124.     TEInit();
  125.     InitDialogs(nil);
  126.     InitCursor();
  127.  
  128.  
  129.         MaxApplZone();
  130.         MoreMasters();                                  /* I want memory, */
  131.         MoreMasters();                                  /* Lots of Memory, */
  132.         MoreMasters();                                  /* I want ApplHeap, and
  133. SysHeap, and more */
  134.         MoreMasters();                                  /* Give me handles, and
  135.     /*
  136.      * setupMenus is execute-once code, so we can unload it now.
  137.      */
  138.     setupMenus();            /* Local procedure, below */
  139.     UnloadSeg(setupMenus);
  140.     /*
  141.      * Calculate the drag rectangle in advance.
  142.      * This will be used when dragging a window frame around.
  143.      * It constrains the area to within 4 pixels from the screen edge
  144.      * and below the menu bar, which is 20 pixels high.
  145.      */
  146.     screenRect = qd.screenBits.bounds;
  147.     SETRECT(&dragRect, 4, 20 + 4, screenRect.right-4, screenRect.bottom-4);
  148.     /*
  149.      * Create our one and only window from the WIND resource.
  150.      * If the WIND resource isn't there, we die.
  151.      */
  152.     myWindow = GetNewWindow(windowID, &wRecord, (WindowPtr) -1);
  153.     SetPort(myWindow);
  154.     /*
  155.      * Create a TextEdit record with the destRect and viewRect set
  156.      * to my window's portRect (offset by 4 pixels on the left and right
  157.      * sides so that text doesn't jam up against the window frame).
  158.      */
  159.     txRect = myWindow->portRect;
  160.     InsetRect(&txRect, 4, 0);
  161.     TextH = TENew(&txRect, &txRect);    /* Not growable, so destRect == viewRect */
  162.  
  163.     ibeamHdl = GetCursor(iBeamCursor);        /* Grab this for use later */
  164.     
  165.     tekmain("didemo2");
  166.     
  167.     /*
  168.      * Ready to go.
  169.      * Start with a clean event slate, and cycle the main event loop
  170.      * until the File/Quit menu item sets DoneFlag.
  171.      *
  172.      * It would not be good practice for the doCommand() routine to
  173.      * simply ExitToShell() when it saw the QuitItem -- to ensure
  174.      * orderly shutdown, satellite routines should set global state,
  175.      * and let the main event loop handle program control.
  176.      */
  177.     DoneFlag = false;
  178.     for ( ;; ) {
  179.         if (DoneFlag) {
  180.             /*
  181.              * Quit has been requested, by the File/Quit menu, or perhaps
  182.              * by a fatal error somewhere else (missing resource, etc).
  183.              * Here we could put up a Save Changes? DLOG, which would also
  184.              * allow the Cancel buttion to set DoneFlag to false.
  185.              */
  186.             break;        /* from main event loop */
  187.         }
  188.         /*
  189.          * Main Event tasks:
  190.          */
  191.         SystemTask();
  192.         theActiveWindow = FrontWindow();        /* Used often, avoid repeated calls */
  193.         /*
  194.          * Things to do on each pass throught the event loop
  195.          * when we are the active window:
  196.          *        [1] Track the mouse, and set the cursor appropriately:
  197.          *            (IBeam if in content region, Arrow if outside)
  198.          *        [2] TEIdle our textedit window, so the insertion bar blinks.
  199.          */
  200.         if (myWindow == theActiveWindow) {
  201.             GetMouse(&mousePt);
  202.             SetCursor(PtInRect(&mousePt, &myWindow->portRect) ? *ibeamHdl : &qd.arrow);
  203.             TEIdle(TextH);
  204.         }
  205.         /*
  206.          * Handle the next event.
  207.          * In a more complex application, this switch statement
  208.          * would probably call satellite routines to handle the
  209.          * major cases (mouseDown, keyDown, etc), but our actions
  210.          * are simple here and it suffices to perform the code in-line.
  211.          */
  212.         if (!GetNextEvent(everyEvent, &myEvent)) {
  213.             /*
  214.              * A null or system event, not for me.
  215.              * Here is a good place for heap cleanup and/or
  216.              * segment unloading if I want to.
  217.              */
  218.             continue;
  219.         }
  220.         /*
  221.          * In the unlikely case that the active desk accessory does not
  222.          * handle mouseDown, keyDown, or other events, GetNextEvent() will
  223.          * give them to us!  So before we perform actions on some events,
  224.          * we check to see that the affected window in question is really
  225.          * our window.
  226.          */
  227.         switch (myEvent.what) {
  228.             case mouseDown:
  229.                 switch (FindWindow(&myEvent.where, &whichWindow)) {
  230.                     case inSysWindow:
  231.                         SystemClick(&myEvent, whichWindow);
  232.                         break;
  233.  
  234.                     case inMenuBar:
  235.                         doCommand(MenuSelect(&myEvent.where));
  236.                         break;
  237.  
  238.                     case inDrag:
  239.                         DragWindow(whichWindow, &myEvent.where, &dragRect);
  240.                         break;
  241.  
  242.                     case inGrow:
  243.                         /* There is no grow box. (Fall through) */
  244.  
  245.                     case inContent:
  246.                         if (whichWindow != theActiveWindow) {
  247.                             SelectWindow(whichWindow);
  248.                         } else if (whichWindow == myWindow) {
  249.                             GlobalToLocal(&myEvent.where);
  250.                             TEClick(&myEvent.where, (myEvent.modifiers & shiftKey) != 0, TextH);
  251.                         }
  252.                         break;
  253.  
  254.                     default:
  255.                         break;
  256.                 }/*endsw FindWindow*/
  257.                 break;
  258.  
  259.             case keyDown:
  260.             case autoKey:
  261.                 if (myWindow == theActiveWindow) {
  262.                     if (myEvent.modifiers & cmdKey) {
  263.                         doCommand(MenuKey(myEvent.message & charCodeMask));
  264.                     } else {
  265.                         TEKey((char) (myEvent.message & charCodeMask), TextH);
  266.                     }
  267.                 }
  268.                 break;
  269.  
  270.             case activateEvt:
  271.                 if ((WindowPtr) myEvent.message == myWindow) {
  272.                     if (myEvent.modifiers & activeFlag) {
  273.                         TEActivate(TextH);
  274.                         DisableItem(MyMenus[editMenu], undoCommand);
  275.                     } else {
  276.                         TEDeactivate(TextH);
  277.                         EnableItem(MyMenus[editMenu], undoCommand);
  278.                     }
  279.                 }
  280.                 break;
  281.  
  282.             case updateEvt:
  283.                 if ((WindowPtr) myEvent.message == myWindow) {
  284.                     BeginUpdate(myWindow);
  285.                     EraseRect(&myWindow->portRect);
  286.                     TEUpdate(&myWindow->portRect, TextH);
  287.                     EndUpdate(myWindow);
  288.                 }
  289.                 break;
  290.  
  291.             default:
  292.                 break;
  293.  
  294.         }/*endsw myEvent.what*/
  295.  
  296.     }/*endfor Main Event loop*/
  297.     /*
  298.      * Cleanup here.
  299.      */
  300.     CloseWindow(myWindow);
  301.     
  302.     return 0;        /* Return from main() to allow C runtime cleanup */
  303. }
  304.  
  305. /*
  306.  * Demonstration of the segmenting facility:
  307.  *
  308.  * This code is execute-once, so we toss it in the "Initialize"
  309.  * segment so that main() can unload it after it's called.
  310.  *
  311.  * There really isn't much here, but it demonstrates the segmenting facility.
  312.  */
  313. /*
  314.  * Set the segment to Initialize.  BEWARE: leading and trailing white space
  315.  * would be part of the segment name!
  316.  */
  317. #define    __SEG__ Initialize
  318.  
  319. /*
  320.  * Set up the Apple, File, and Edit menus.
  321.  * If the MENU resources are missing, we die.
  322.  */
  323. void setupMenus()
  324. {
  325.     extern        MenuHandle    MyMenus[];
  326.     register    MenuHandle    *pMenu;
  327.  
  328.     /*
  329.      * Set up the desk accessories menu.
  330.      * The "About Sample..." item, followed by a grey line,
  331.      * is presumed to be already in the resource.  We then
  332.      * append the desk accessory names from the 'DRVR' resources.
  333.      */
  334.     MyMenus[appleMenu] = GetMenu(appleID);
  335.     AddResMenu(MyMenus[appleMenu], (ResType) 'DRVR');
  336.     /*
  337.      * Now the File and Edit menus.
  338.      */
  339.     MyMenus[fileMenu] = GetMenu(fileID);
  340.     MyMenus[editMenu] = GetMenu(editID);
  341.     /*
  342.      * Now insert all of the application menus in the menu bar.
  343.      *
  344.      * "Real" C programmers never use array indexes
  345.      * unless they're constants :-)
  346.      */
  347.     for (pMenu = &MyMenus[0]; pMenu < &MyMenus[menuCount]; ++pMenu) {
  348.         InsertMenu(*pMenu, 0);
  349.     }
  350.  
  351.     DrawMenuBar();
  352.  
  353.     return;
  354. }
  355.  
  356. /*
  357.  * Back to the Main segment.
  358.  */
  359. #define    __SEG__ Main
  360.  
  361. /*
  362.  * Display the Sample Application dialog.
  363.  * We insert two static text items in the DLOG:
  364.  *        The author name
  365.  *        The source language
  366.  * Then wait until the OK button is clicked before returning.
  367.  */
  368. void showAboutMeDialog()
  369. {
  370.     GrafPtr     savePort;
  371.     DialogPtr    theDialog;
  372.     short        itemType;
  373.     Handle        itemHdl;
  374.     Rect        itemRect;
  375.     short        itemHit;
  376.  
  377.     GetPort(&savePort);
  378.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  379.     SetPort(theDialog);
  380.  
  381.     GetDItem(theDialog, authorItem, &itemType, &itemHdl, &itemRect);
  382.     SetIText(itemHdl, "Flash Bazbo");
  383.     GetDItem(theDialog, languageItem, &itemType, &itemHdl, &itemRect);
  384.     SetIText(itemHdl, "C");
  385.  
  386.     do {
  387.         ModalDialog(nil, &itemHit);
  388.     } while (itemHit != okButton);
  389.  
  390.     CloseDialog(theDialog);
  391.  
  392.     SetPort(savePort);
  393.     return;
  394. }
  395. /*
  396.  * Process mouse clicks in menu bar
  397.  */
  398. void doCommand(mResult)
  399.     long    mResult;
  400. {
  401.     int                 theMenu, theItem;
  402.     char                daName[256];
  403.     GrafPtr             savePort;
  404.     extern MenuHandle    MyMenus[];
  405.     extern Boolean        DoneFlag;
  406.     extern TEHandle     TextH;
  407.     extern void         showAboutMeDialog();
  408.  
  409.     theItem = LOWORD(mResult);
  410.     theMenu = HIWORD(mResult);        /* This is the resource ID */
  411.  
  412.     switch (theMenu) {
  413.         case appleID:
  414.             if (theItem == aboutMeCommand) {
  415.                 showAboutMeDialog();
  416.             } else {
  417.                 GetItem(MyMenus[appleMenu], theItem, daName);
  418.                 GetPort(&savePort);
  419.                 (void) OpenDeskAcc(daName);
  420.                 SetPort(savePort);
  421.             }
  422.             break;
  423.  
  424.         case fileID:
  425.             switch (theItem) {
  426.                 case quitCommand:
  427.                     DoneFlag = true;            /* Request exit */
  428.                     break;
  429.                 default:
  430.                     break;
  431.             }
  432.             break;
  433.  
  434.         case editID:
  435.             /*
  436.              * If this is for a 'standard' edit item,
  437.              * run it through SystemEdit first.
  438.              * SystemEdit will return FALSE if it's not a system window.
  439.              */
  440.             if ((theItem <= clearCommand) && SystemEdit(theItem-1)) {
  441.                 break;
  442.             }
  443.             /*
  444.              * Otherwise, it's my window.
  445.              * Handle Cut/Copy/Paste properly
  446.              * between the TEScrap and the Clipboard.
  447.              */
  448.             switch (theItem) {
  449.                 case undoCommand:
  450.                     /* can't undo */
  451.                     break;
  452.                 case cutCommand:
  453.                 case copyCommand:
  454.                     if (theItem == cutCommand) {
  455.                         TECut(TextH);
  456.                     } else {
  457.                         TECopy(TextH);
  458.                     }
  459.                     ZeroScrap();
  460.                     TEToScrap();
  461.                     break;
  462.                 case pasteCommand:
  463.                     TEFromScrap();
  464.                     TEPaste(TextH);
  465.                     break;
  466.                 case clearCommand:
  467.                     TEDelete(TextH);
  468.                     break;
  469.                 default:
  470.                     break;
  471.             } /*endsw theItem*/
  472.             break;
  473.  
  474.         default:
  475.             break;
  476.  
  477.     }/*endsw theMenu*/
  478.  
  479.     HiliteMenu(0);
  480.  
  481.     return;
  482. }
  483.  
  484.  
  485. /****************************************************************************************/
  486. /*   new stuff 
  487. */
  488. #define BUFSIZE 6000
  489. #include "stdio.h"
  490.  
  491. PicHandle
  492.     RGtoPICT();             /* Convert Drawings to pictures */
  493.  
  494. char testbuf[BUFSIZE]; /* text input buffer */
  495. FILE *plot;
  496.  
  497. writeln(c)
  498. char *c;
  499. {
  500.     fprintf(plot,"%s",c);
  501. }
  502.  
  503. charout(c)
  504. char c;
  505. { fputc(c,plot); }
  506.  
  507.  
  508.  
  509. tekmain(argv)
  510. char *argv;
  511. {
  512.     int w1;
  513.  
  514.     VGinit();
  515.     plot=fopen("tekout.x","w");
  516. /*    RGPoutfunc(writeln);
  517.     RGHPoutfunc(writeln);
  518.     RGEPoutfunc(charout);*/
  519.  
  520.     w1=VGnewwin(0);
  521.  
  522. /*    VGuncover(w1);  */
  523.     VGpage(w1);
  524.     showfile(w1,argv); /* 1st param is filepath */
  525.  
  526. /*
  527.     VGpage(w1);
  528.     drawall(w1);
  529.     VGpage(w1);
  530.     VGredraw(w1,w1);
  531. */
  532. {
  533.     long tlong;                 /* Temporary Variable */
  534.     PicHandle tpic;             /* Mental picture of the thing */
  535.  
  536.     tpic=RGtoPICT(w1);    /* Get the picture */
  537.     LoadScrap();
  538.     tlong=ZeroScrap();              /* Nobody else can live here */
  539.     HLock(tpic);                    /* Lock it for Puting */
  540.     tlong=PutScrap(GetHandleSize(tpic),'PICT',*tpic);   /* Store as a PICT */
  541.     HUnlock(tpic);                  /* Unlock so we can toss it */
  542.     KillPicture(tpic);              /* Kill the picture..... */
  543. }    
  544. /*    VGtmode(w1);    */
  545.     VGclose(w1);
  546.  
  547. } /* end main() */
  548.  
  549.  
  550. static drawall(vw)
  551. /* redraw the whole thing incrementally */
  552. {
  553.     int i;
  554.     while (!(i=VGpred(vw,vw)));
  555. }
  556.  
  557.  
  558. static showfile(vw,fname)
  559. int vw;
  560. char *fname;
  561. /*
  562.     Read the file named by fname and display it in the window.
  563.     MODIFICATION NEEDED to check for escape form feed to stop newframe advance
  564. */
  565. {
  566.     int datalen,i;
  567.     FILE *fp = fopen(fname,"r");
  568.     if (fp != NULL) {
  569.         while ((datalen = fread(testbuf,1,BUFSIZE,fp)) > 0) {
  570.             for (i=0; i<datalen ; i++)
  571.                 VGwrite(vw,&testbuf[i], 1);
  572.         }
  573.         fclose(fp);
  574.     }
  575. }
  576.  
  577. PicHandle RGtoPICT(i)
  578. int i;
  579. {
  580.     int j;
  581.     PicHandle tpic;
  582.     Rect trect;
  583.  
  584.     SetRect(&trect,0,0,200,200);
  585.     j=VGnewwin(3);
  586.     RGMPsize( &trect );
  587.     VGzcpy( i, j);             /* Love dat zm factr */
  588.     ClipRect(&trect);
  589.     ShowPen();
  590.     tpic=OpenPicture(&trect);
  591.     VGredraw(i,j);
  592.     ClosePicture();
  593.     VGclose(j);
  594.     HidePen();
  595.  
  596.     return(tpic);
  597. }
  598.  
  599.  
  600.